package test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * description: <br>
 * createTime: 2020/11/311:49 <br>
 *
 * @author zzh
 */
public class ThreadDemo {

    public static void useThread() {
        new Thread() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }.start();
    }

    public static void useRunnable() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();
    }

    public static void useCallable() throws Exception {
        Callable<String> callable = new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                return "call back";
            }
        };
        FutureTask<String> ft = new FutureTask<>(callable);
        new Thread(ft, "有返回值的线程").start();
        System.out.println("子线程的返回值：" + ft.get());
    }

    public static void useExecuteService() {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.submit( () -> {
            System.out.println(Thread.currentThread().getName());
        });
        exec.shutdown();
    }

    public static void main(String[] args) throws Exception {
        useThread();
        useCallable();
        useRunnable();
        useExecuteService();
        System.out.println(Thread.currentThread().getName());
    }
}
